package de.wolvan.window;

import javax.swing.*;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URISyntaxException;

import de.wolvan.main.main;
import de.wolvan.references.*;

public class LoginWindow extends JFrame implements ActionListener{
	private JButton exit;
	private static JButton login;
	private JButton info;
	
	private JLabel USERNAME;
	private JLabel PIN;
	
	public static JTextField username;
	public static JTextField pin;
	
	public static LoginWindow frame;
	
	public static boolean loginEnabled = true;
	
	public static void renderGUI() {
		frame = new LoginWindow("Login");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400,270);
		frame.setLayout(null);
		frame.setVisible(true);
	}
	
	public static void enableLogin() {
		if(!loginEnabled) {
			login.setEnabled(true);
			username.setEnabled(true);
			pin.setEnabled(true);
			loginEnabled = true;
		}
	}
	
	public static void disableLogin() {
		if(loginEnabled) {
			login.setEnabled(false);
			username.setEnabled(false);
			pin.setEnabled(false);
			loginEnabled = false;
		}
	}
	
	public LoginWindow(String title) {
		super(title);

		exit = new JButton("Exit");
		exit.setBounds(10,130,170,40);
		exit.addActionListener(this);
		add(exit);
		
		login = new JButton("Login");
		login.setBounds(190,130,180,40);
		login.addActionListener(this);
		add(login);
		
		info = new JButton("Program Information");
		info.setBounds(10,180,360, 40);
		info.addActionListener(this);
		add(info);
		
		username = new JTextField("");
		username.setBounds(10,30,360,20);
		username.addActionListener(this);
		add(username);
		
		pin = new JTextField("");
		pin.setBounds(10,90,360,20);
		pin.addActionListener(this);
		add(pin);
		
		USERNAME = new JLabel("Username:");
		USERNAME.setBounds(10,10,360,20);
		add(USERNAME);
		
		PIN = new JLabel("PIN:");
		PIN.setBounds(10,70,360,20);
		add(PIN);
		
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == username) {
			pin.requestFocus(false);
		}
		if(e.getSource() == pin) {
			login.doClick();
		}
		if(e.getSource() == exit) {
			System.exit(0);
		}
		if(e.getSource() == login) {
			username.setText(username.getText().toLowerCase());
			if(!username.getText().equals("")) {
				disableLogin();
				if(pin.getText().equals("")) pin.setText("0000");
				try {
					main.openWS();
				} catch (URISyntaxException e1) {
					e1.printStackTrace();
				}
			} else {
				JOptionPane.showMessageDialog(null, "You have to enter a username and a PIN!\nThe PIN is made out of 4 numbers!", "Blank Fields", 0, null);
			}
		}
		if (e.getSource() == info) {
				//JOptionPane.showMessageDialog(null, new JEditorPane("<HTML>" + metadata.name + " " + metadata.version + "\nProgrammed by " + metadata.developer + "\n<a href=\"https://code.google.com/p/ximd/\">Google Code</a>\nContact: wolvan1@gmail.com\n" + metadata.additionalInfoText + "</HTML>"), "Information", 1, null);
				InfoWindow.renderGUI();
		}
	}
}
